package com.lww.feature.test.sizeofobject;

import com.lww.sizeofobject.SizeOfObject;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;

/**
 *测试步骤:
 * 1、打包sizeofobject，需要配置pom文件中的manifestEntries属性。
 * 2、设置vm：
 * -javaagent:/Users/lww/.m2/repository/com/lww/SizeOfObject/1.0-SNAPSHOT/SizeOfObject-1.0-SNAPSHOT.jar
 *
 * User: yian
 * Date: 2017/5/11
 */
public class test {
    public static void main(String[] args) {
        LinkedList<SettlePayDay> lists = new LinkedList<>();
        int num = 0;
        while (num < 10000) {
            SettlePayDay settlePayDay = new SettlePayDay();
            settlePayDay.setAccountId(Math.random()+"");
            settlePayDay.setInAmount((long)(Math.random()*100000));
            settlePayDay.setInCount((int)(Math.random()*10));
            settlePayDay.setPayDate(new Date());
            settlePayDay.setOrderSource((int)(Math.random()*30));
            settlePayDay.setRootTypeCode(Math.random()+"");
            lists.add(settlePayDay);
            num ++;
        }

        System.out.println(SizeOfObject.sizeOf(lists));
        System.out.println(SizeOfObject.fullSizeOf(lists));
    }
    public static void main1(String[] args) throws IllegalAccessException {

        String a = "";
        String b = "11111111111111122222222222222222222333333333333333";
        String c = "11111111111111122222222222222222222333333333333333eeeeeeeeeeeeee" +
                "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeewwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww" +
                "wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww" +
                "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" +
                "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" +
                "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr";
        System.out.println(SizeOfObject.sizeOf(a));
        System.out.println(SizeOfObject.sizeOf(b));
        System.out.println(SizeOfObject.sizeOf(c));
        System.out.println(SizeOfObject.fullSizeOf(a));
        System.out.println(SizeOfObject.fullSizeOf(b));
        System.out.println(SizeOfObject.fullSizeOf(c));
        LinkedList<Integer> integers = new LinkedList<>();
        int num = Integer.MAX_VALUE;
        while (num > Integer.MAX_VALUE - 1000000) {
            integers.add(num--);
        }
        System.out.println(Runtime.getRuntime().maxMemory());
        System.out.println("size:"+integers.size());
        System.out.println("last:" + integers.getLast());
        System.out.println("first:" + integers.getFirst());
        System.out.println(SizeOfObject.sizeOf(integers));
        System.out.println(SizeOfObject.fullSizeOf(integers));

    }
//3817865216
//64487424
    @Test
    public void testIntegerList() {
        LinkedList<Integer> integers = new LinkedList<>();
        int num = Integer.MAX_VALUE;
        while (num > Integer.MAX_VALUE - 1) {
            integers.add(num--);
        }
        System.out.println(Runtime.getRuntime().maxMemory());
        System.out.println("size:"+integers.size());
        System.out.println("last:" + integers.getLast());
        System.out.println("first:" + integers.getFirst());
        System.out.println(SizeOfObject.sizeOf(integers));
        System.out.println(SizeOfObject.fullSizeOf(integers));
    }//64487424
    @Test
    public void testIntegerList2() {
        ArrayList<Integer> integers = new ArrayList<>();
        int num = Integer.MAX_VALUE;
        while (num > Integer.MAX_VALUE - 1000000) {
            integers.add(num--);
        }
        System.out.println(Runtime.getRuntime().maxMemory());
        System.out.println("size:"+integers.size());
        System.out.println("last:" + integers.get(0));
        System.out.println("first:" + integers.get(integers.size()-1));
        System.out.println(SizeOfObject.sizeOf(integers));
        System.out.println(SizeOfObject.fullSizeOf(integers));
    }

    @Test
    public void testIntegerAndDateList() throws InterruptedException {
        LinkedList<IntegerAndDate> integerAndDates = new LinkedList<>();
        int num = Integer.MAX_VALUE;

        System.out.println("已用内存" + Runtime.getRuntime().totalMemory());
        System.out.println("最大内存" + Runtime.getRuntime().maxMemory());
        System.out.println("可用内存" + Runtime.getRuntime().freeMemory());
        while (num > Integer.MAX_VALUE - 100000) {
            IntegerAndDate integerAndDate = new IntegerAndDate();
            integerAndDate.setInteger(num--);
            integerAndDate.setDate(new Date());
            integerAndDates.add(integerAndDate);
        }
        Thread.sleep(1000);
        System.out.println("------------------");
        System.out.println("已用内存" + Runtime.getRuntime().totalMemory());
        System.out.println("最大内存" + Runtime.getRuntime().maxMemory());
        System.out.println("可用内存" + Runtime.getRuntime().freeMemory());
        System.out.println(Runtime.getRuntime().maxMemory());
        System.out.println("size:"+integerAndDates.size());
        System.out.println("last:" + integerAndDates.getLast().getInteger());
        System.out.println("first:" + integerAndDates.getFirst().getInteger());
        System.out.println(SizeOfObject.sizeOf(integerAndDates));
        System.out.println(SizeOfObject.fullSizeOf(integerAndDates));
    }
}
